<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      /**
       * @param {number[][]} intervals
       * @return {number[][]}
       */
      var merge = function(intervals) {
        if(intervals.length<=1) {
          return intervals;
        }
        intervals.sort((a,b)=>a[0]-b[0])
        let initArr = [intervals[0]]
        return intervals.reduce((prev, cur, index)=>{
          let size = prev.length;
          let lastRight = prev[size-1]
          if(lastRight[1] < cur[0]) {
            prev.push(cur)
          } else {
            prev[size-1][1] = Math.max(cur[1], lastRight[1])
          }
          return prev
        },initArr)
      };

    </script>
  </body>
</html>
